package com.wqnmdb.im.controller;

import com.wqnmdb.im.domain.resp.R;
import com.wqnmdb.im.domain.resp.RespUtils;
import com.wqnmdb.im.service.minio.MinioService;
import io.minio.Result;
import io.minio.messages.Upload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "/file", tags = {"文件操作"})      // 类注解
public class FileController {

    @Autowired
    private MinioService minioService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件", notes = "注意文件大小")
    public ResponseEntity<R> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return RespUtils.fail("file为空或不存在.");
        }
        // 获取文件名称
        String filename = file.getOriginalFilename();
        log.info("获取的文件名称 ： " + filename);
        // 获取文件大小
        long size = file.getSize();
        log.info("获取的文件大小 ： " + size + "b");

        String uploadUrl = minioService.upload("test", file);
        return RespUtils.success(uploadUrl);
    }

    @PostMapping("/getUrlByName")
    @ApiOperation(value = "获取链接", notes = "通过文件名获取链接")
    public ResponseEntity<R> getUrlByName(@RequestParam("name") String fileName) {
        log.info("获取的文件名称 ： " + fileName);
        String fileUrl = minioService.getFileUrl("test", fileName, 60);
        return RespUtils.success(fileUrl);
    }

    @PostMapping("/getUrlByNameV2")
    @ApiOperation(value = "获取链接(有失效时间)", notes = "通过文件名获取链接")
    public ResponseEntity<R> getUrlByNameV2(@RequestParam("name") String fileName, @RequestParam("expires") Integer expires) {
        log.info("获取的文件名称 ： " + fileName);
        String fileUrl = minioService.getFileUrl("test", fileName, expires);
        return RespUtils.success(fileUrl);
    }

    @PostMapping("/downloadByName")
    @ApiOperation(value = "下载文件", notes = "带文件后缀")
    public ResponseEntity<R> downloadByName(@RequestParam("name") String fileName, HttpServletResponse response) {
        log.info("获取的文件名称 ： " + fileName);
        try {
            minioService.download("test", fileName, response);
        }catch (Exception e){
            return RespUtils.fail("文件不存在！");
        }
        return null;
    }

    @PostMapping("/wcfUpload")
    @ApiOperation(value = "获取断点", notes = "wcfUpload")
    public ResponseEntity<R> wcfUpload(@RequestParam("prefix") String prefix) {
        Iterable<Result<Upload>> results = minioService.wcfUpload("test", prefix, true);
        return RespUtils.success(results);
    }
}
